//
//  NextListViewController.swift
//  BusinessIOS
//
//  Created by Rain on 2018/11/12.
//  Copyright © 2018 XingJian Li. All rights reserved.
//

import UIKit
import Alamofire


class NextListViewController: UIViewController {
     var tableView: UITableView!
    
    var bigNextListArray = [NextListModel]()
    var NextListArray = [NextListModel]()
    
    // 顶部刷新
    let header = MJRefreshNormalHeader()
    // 底部刷新
    let footer = MJRefreshAutoNormalFooter()
     var index = 1
        override func viewDidLoad() {
            super.viewDidLoad()
            
           
            self.view.backgroundColor = UIColor.white
            tableView = UITableView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height), style: .plain )
            tableView.dataSource = self
            tableView.delegate = self
            tableView.separatorInset = UIEdgeInsets.zero
            //        tableView.separatorStyle = .none
            tableView.backgroundColor = bgGrayColor
            tableView.showsHorizontalScrollIndicator = false
            tableView.showsVerticalScrollIndicator = false
            //tableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0)//去除表格上放多余的空隙
            //tableView.tableHeaderView = UIView.init()
            tableView.tableFooterView = UIView.init()
            self.view.addSubview(tableView)
            
            
            self.loadData()
            
            
            // 下拉刷新
            header.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
            // 现在的版本要用mj_header
            tableView.mj_header = header
            
            
            // 上拉刷新
            footer.setRefreshingTarget(self, refreshingAction: #selector(footerRefresh))
            tableView.mj_footer = footer
            
            
            
         
        }
    
    func loadData() {
        
        self.NextListArray.removeAll()
        self.bigNextListArray.removeAll()
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(16)\",\"start\":\"\(1)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("2222222222json====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = NextListModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    self.NextListArray.append(model1)
                    
                    
                    print("NextListArray====\(self.NextListArray.count)")
                    
                }
                
                self.bigNextListArray.append(contentsOf: self.NextListArray)
                
                
                self.tableView.reloadData()
                
                self.tableView.mj_header.endRefreshing()
                
            }else{
                
                print("数据请求失败")
            }
            
            
            
            
        }
    }
    
    @objc func footerRefresh(){
        
        self.NextListArray.removeAll()
        
        index += 1
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(16)\",\"start\":\"\(index)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
        
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
            print("2222222222json====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = NextListModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
                    
                    self.NextListArray.append(model1)
                    
                    
                    print("NextListArray====\(self.NextListArray.count)")
                    
                }
                
                self.bigNextListArray.append(contentsOf: self.NextListArray)
                
                
                self.tableView.reloadData()
                
                self.tableView.mj_footer.endRefreshing()
                
            }else{
                
                print("数据请求失败")
            }
            
            
            
            
        }
    }
    
    
     @objc func headerRefresh(){
        
        
        self.NextListArray.removeAll()
        self.bigNextListArray.removeAll()
        
        let urlString = "http://wwwqjzsyzcscom.aykj.net.cn/api/article/getArticleList"
        
        let json = "{\"newsClassId\":\"\(16)\",\"start\":\"\(0)\",\"pageSize\":\"\(5)\"}"
        
        //             let json = "{\"account\":\"\(phoneTextField.text!)\",\"password\":\"\(PasswordTextField.text!)\",\"validateCode\":\"\(verificationTextField.text!)\"}"
      
        
        let url = URL(string: urlString)!
        let jsonData = json.data(using: .utf8, allowLossyConversion: false)!
        
        var request = URLRequest(url: url)
        request.httpMethod = HTTPMethod.post.rawValue
        request.setValue("application/json", forHTTPHeaderField: "Content-Type")
        request.httpBody = jsonData
        
        Alamofire.request(request).responseJSON {(response) in
            
              print("2222222222json====\(response)")
            if response.result.isSuccess {
                let json = JSON(response.data!)
                let array = json["resultData"].array
                
                
                for dataDic in  array! {
                    
                    let model1 = NextListModel  ()
                    model1.articleDetail = dataDic["articleDetail"].string
                    
                    model1.createtime = dataDic["createtime"].string
                    model1.title = dataDic["title"].string
                    
                    model1.id = dataDic["id"].string
  
                    self.NextListArray.append(model1)
                    
                    
                    print("NextListArray====\(self.NextListArray.count)")
                    
                }
                
                self.bigNextListArray.append(contentsOf: self.NextListArray)
                
                
                self.tableView.reloadData()
                
                self.tableView.mj_header.endRefreshing()
                
            }else{
                
                print("数据请求失败")
            }
            
     
            
     
        }
        
    }
 

        

}
extension NextListViewController: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.bigNextListArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 40
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        
            let nextListModel:NextListModel = self.bigNextListArray[indexPath.row]
        
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
        if cell == nil {
            
            cell = UITableViewCell.init(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
        }
        cell?.textLabel?.text = nextListModel.title
        cell?.textLabel?.font = UIFont.systemFont(ofSize:12)
        cell?.detailTextLabel?.text = nextListModel.createtime
        cell?.detailTextLabel?.font = UIFont.systemFont(ofSize: 12)
        return cell!
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
         let nextListModel:NextListModel = self.bigNextListArray[indexPath.row]
        
        let WebVC =  WebViewController()
        WebVC.numInteger = nextListModel.id
        self.navigationController?.pushViewController(WebVC , animated: true)
        
    }
    
}
